﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace QuanLyKhachSan_DAL_WS
{
    public class XuLyChucVu
    {
        // tim toan bo chuc vu
        public static DataTable TimToanBoChucVu()
        {
            CDataProvider reader = new CDataProvider();

            // chuoi SELECT
            String sql = " SELECT * FROM CHUCVU ";

            DataTable dt = new DataTable();
            try
            {
                dt = reader.DocDuLieu(sql);
            }
            catch (System.Exception )
            {
            	
            }

            return dt;
        }
        // tim 1 chuc vu theo MaLoaiNhanVien
        public static CCHUCVU TimLoaiNVTheoMaLoai(String MaLoaiNV)
        {
            CCHUCVU cv = new CCHUCVU();
            // lop DataProvider
            CDataProvider reader = new CDataProvider();
            // chuoi SELECT
            String sql = " SELECT * FROM CHUCVU " +
                " WHERE MaLoaiNV = '" + MaLoaiNV.ToString() + "' ";

            try
            {
                // Bang data se duoc nhap
                DataTable dt = reader.DocDuLieu(sql);

                // Neu ton tai phong nay
                if (dt.Rows.Count == 1)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        cv.MaLoaiNhanVien = dr["MaLoaiNV"].ToString();
                        cv.LoaiNhanVien = dr["LoaiNhanVien"].ToString();
                    }
                }
                // phong nay ko ton tai
                else
                {
                    cv.MaLoaiNhanVien = " ";
                    cv.LoaiNhanVien = "khong tim thay";
                }
            }
            catch (System.Exception )
            {
            	
            }

            return cv;
        }
        // them 1 chuc vu
        public static Boolean ThemChucVu(CCHUCVU cv)
        {
            CDataProvider writer = new CDataProvider();

            // Cau lenh SQL
            String insertSQL = " INSERT INTO CHUCVU(MaLoaiNV,LoaiNhanVien) "
                + " VALUES('" + cv.MaLoaiNhanVien.ToString() + "',"
                + cv.LoaiNhanVien.ToString() + ") ";

            // Thuc thi cau lenh insert
            if (writer.ThucThiDuLieu(insertSQL))
                return true;
            else
                return false;
        }
        // sua thong tin 1 chuc vu
        public static Boolean SuaTTChucVu(CCHUCVU cv, String MaLoaiNV)
        {
            CDataProvider writer = new CDataProvider();

            // Cau lenh SQL
            String updateSQL = " UPDATE CHUCVU "
                + " SET MaLoaiNV = '" + cv.MaLoaiNhanVien.ToString() + "', "
                + " LoaiNhanVien = " + cv.LoaiNhanVien.ToString() + " "
                + " WHERE MaLoaiNV ='" + MaLoaiNV.ToString() + "' ";

            // Thuc thi cau lenh insert
            if (writer.ThucThiDuLieu(updateSQL))
                return true;
            else
                return false;
        }
        // xoa 1 chuc vu
        public static Boolean XoaChucVu(String MaLoaiNV)
        {
            CDataProvider writer = new CDataProvider();

            // Cau lenh SQL
            String deleteSQL = " DELETE FROM CHUCVU "
                + " WHERE MaLoaiNV ='" + MaLoaiNV + "' ";

            // Thuc thi cau lenh insert
            if (writer.ThucThiDuLieu(deleteSQL))
                return true;
            else
                return false;
        }
    }
}
